<template>
  <div class="shopCar-container">
    <!-- 商品列表 -->
    <div class="list">
      <!-- 循环 begin -->
      <div class="mui-card" v-for="(item,i) in shopCarList" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <!-- 按钮 -->
            <mt-switch 
            v-model="$store.getters.getGoodsSelected[item.id]"
            @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
            <!-- 图片 -->
            <img :src="item.thumb_path" alt="">
            <!-- 购买数量 begin -->
            <div class="carList">
              <!-- 商品标题 -->
              <h1>{{item.title}}</h1>
              <!-- 商品删除 -->
              <div class="info">
                <span>￥{{item.sell_price}}</span>
                <numbox :initcount="$store.getters.getShopCarCount[item.id]" :goodsId="item.id"></numbox>
                <a class="del" @click.prevent="remove(item.id , i)">删除</a>
              </div>
            </div>
            <!-- 购买数量 end -->
          </div>
        </div>
      </div>
      <!-- 循环 end -->
    </div>
    <!-- 商品结算 begin -->
    <div class="carClearing">
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner carC-mui">
            <!-- 左边 -->
            <div class="carC_L">
              <p>总计（不含运费）</p>
              <p>已勾选商品
                <span class="red"> {{$store.getters.getGoodsCountAndAmount.count}} </span>件,总价
                <span class="red">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
              </p>
            </div>
            <!-- 右边 -->
            <mt-button type="danger">去结算</mt-button>
          </div>
        </div>
      </div>
    </div>
    <!-- 商品结算 end -->


  </div>
</template>
<script>
// 购买输入框
import numbox from "../subcomments/shopCar_numbox.vue";
export default {
    data() {
        return {
            shopCarList: []
        };
    },
    created() {
        this.getShopCarList();
    },
    methods: {
        // 获取购物车列表
        getShopCarList() {
            var idArr = [];
            this.$store.state.car.forEach(item => {
                idArr.push(item.id);
            });
            // 如果本地存储没有 就返回
            if (idArr.length <= 0) {
                return false;
            }
            // 获取数据
            this.$http
                .get("api/goods/getshopcarlist/" + idArr.join(","))
                .then(result => {
                    if (result.body.status === 0) {
                        this.shopCarList = result.body.message;
                    }
                });
        },
        // 刪除購物車列表數據
        remove(id, i) {
            // 刪除 shopCarList 麗的數據
            this.shopCarList.splice(i, 1);
            // 刪除遠程的原數據
            this.$store.commit("delFromCar", { id });
        },
        // 购物车页面选中商品事件
        selectedChanged(id,val){
            console.log(
                'id: ' + id + '=============='
                +'val: ' + val
            );
            this.$store.commit('upDateShopCarSelected',{
                id,
                selected: val
            })
        }
    },
    components: {
        numbox
    }
};
</script>
<style lang="scss">
.shopCar-container {
    overflow: hidden;
    background-color: #f1f1f1;
    .list {
        .mui-card-content-inner {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .carList {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
            }
            .info {
                align-items: center;
                display: flex;
                justify-content: space-between;
                span {
                    color: red;
                    font-weight: 700;
                }
            }
            img {
                width: 60px;
                height: 60px;
            }
            h1 {
                font-size: 13px;
            }
        }
    }

    .carClearing {
       
        
        .carC-mui {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .red {
                color: red;
                font-weight: 700;
            }
            p {
                font-size: 12px;
            }
        }
    }
}
</style>
